package com.gurun.gateway;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.gurun.base.Logger;
import com.gurun.base.utils.CommonUtils;
import com.gurun.model.bean.gateway.command.CommandDataModel;
import com.gurun.model.bean.gateway.command.GateWayCommandFromPushModel;
import com.gurun.model.bean.gateway.command.GateWayCommandModel;

/**
 * Created by harry on 2018/3/16.
 */

public class CommandFactory {
    public static String getEnableReadOkReportCommand(Context context) {
        Gson gson = new Gson();
        CommandDataModel commandDataModel = new CommandDataModel();
        commandDataModel.setStatus("announce");
        String data = gson.toJson(commandDataModel);
        GateWayCommandModel gateWayCommandModel = new GateWayCommandModel();
        gateWayCommandModel.setCmd("report");
        gateWayCommandModel.setModel("stbgw");
        gateWayCommandModel.setSid(CommonUtils.getAndoridId(context));
        gateWayCommandModel.setShort_id(0);
        gateWayCommandModel.setToken("3");
        gateWayCommandModel.setData(data);
        return gson.toJson(gateWayCommandModel);
    }

    public static String getEnableReadOkHeartbeatCommand(Context context) {
        Gson gson = new Gson();
        CommandDataModel commandDataModel = new CommandDataModel();
        commandDataModel.setStatus("on");
        String data = gson.toJson(commandDataModel);
        GateWayCommandModel gateWayCommandModel = new GateWayCommandModel();
        gateWayCommandModel.setCmd("heartbeat");
        gateWayCommandModel.setModel("stbgw");
        gateWayCommandModel.setSid(CommonUtils.getAndoridId(context));
        gateWayCommandModel.setShort_id(0);
        gateWayCommandModel.setToken("3");
        gateWayCommandModel.setData(data);
        return gson.toJson(gateWayCommandModel);
    }

    public static GateWayCommandModel stringCommandToModel(String command) {
        if (TextUtils.isEmpty(command)) {
            return null;
        }
        try {
            Gson gson = new Gson();
            GateWayCommandModel gateWayCommandModel = gson.fromJson(command, GateWayCommandModel.class);
            return gateWayCommandModel;
        } catch (Exception e) {
            Logger.e(e.getMessage());
        }
        return null;
    }

    public static GateWayCommandFromPushModel.CommandModel stringCommandToModelForPush(String command) {
        if (TextUtils.isEmpty(command)) {
            return null;
        }
        try {
            Gson gson = new Gson();
            GateWayCommandFromPushModel gateWayCommandModel = gson.fromJson(command, GateWayCommandFromPushModel.class);
            String data = gateWayCommandModel.getData();
            if (!TextUtils.isEmpty(data)){
                GateWayCommandFromPushModel.CommandModel commandModel =  gson.fromJson(data, GateWayCommandFromPushModel.CommandModel.class);
                return commandModel;
            }
        } catch (Exception e) {
//            Logger.e(e.getMessage());
        }
        return null;
    }

    public static String getRemoveCommand(Context context, GateWayCommandModel commandModel) {
        Gson gson = new Gson();
        GateWayCommandModel gateWayCommandModel = new GateWayCommandModel();
        gateWayCommandModel.setCmd("remove_device");
        gateWayCommandModel.setModel(commandModel.getModel());
        gateWayCommandModel.setSid(commandModel.getSid());
        gateWayCommandModel.setShort_id(commandModel.getShort_id());
        gateWayCommandModel.setToken(commandModel.getToken());
        gateWayCommandModel.setData(commandModel.getData());
        return gson.toJson(gateWayCommandModel);
    }

    public static String getJoinCommand(Context context, GateWayCommandModel commandModel) {
        Gson gson = new Gson();
        GateWayCommandModel gateWayCommandModel = new GateWayCommandModel();
        gateWayCommandModel.setCmd("zigbee_join");
        gateWayCommandModel.setModel(commandModel.getModel());
        gateWayCommandModel.setSid(commandModel.getSid());
        gateWayCommandModel.setShort_id(commandModel.getShort_id());
        gateWayCommandModel.setToken(commandModel.getToken());
        gateWayCommandModel.setData(commandModel.getData());
        return gson.toJson(gateWayCommandModel);
    }
}
